<?php
include('../../include/functions.inc.php');
$user = unserialize($_SESSION['user']);
$_SESSION["invoice"] = null;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Asignacion de Estudiantes</title>
        <link href="../../bootstrap/css/bootstrap.css" rel="stylesheet" type="text/css" />
        <link href="../../css/ContentStyle.css" rel="stylesheet" type="text/css" />
        <link href="../../css/tableorderer.css" rel="stylesheet" type="text/css" />
        <link href="../../js/protocalendar/datepicker.css" rel="stylesheet" type="text/css" />
        <link href="../../js/dialog/dialog.2.0.css" rel="stylesheet" type="text/css" />
        <script src="../../js/jquery.js" type="text/javascript"></script>
        <script>jQuery.noConflict();</script>
        <script src="../../bootstrap/js/bootstrap.js"></script>
        <script src="../../js/prototype.js" type="text/javascript"></script>
        <script src="../../js/protocalendar/prototype-date-extensions.js" type="text/javascript"></script>
        <script src="../../js/protocalendar/datepicker.js"  type="text/javascript"></script>
        <script src="../../js/jsvalidate.js" type="text/javascript"></script>
        <script src="../../js/TableOrderer.js" type="text/javascript"></script>
        <script src="../../js/dialog/dialog.2.0.js" type="text/javascript"></script>
        <script src="../../js/include.js" type="text/javascript"></script>
        <style type="text/css">
            input,
            textarea{
                width: 300px;
            }
            select{
                width: 315px;
            }
        </style>
        <script type="text/javascript">
            var cargos = new Hash();
            var moneda = null;

            doit = function(c, d) {
                d.checked = c.checked;
                txt = "txt_" + d.ID;
                $(txt).readOnly = !c.checked;
                if (c.checked) {
                    moneda = moneda === null ? d.MONEDA : moneda;
                    if (d.MONEDA !== moneda) {
                        alert("Solo puede seleccionar cargos con la misma moneda!");
                        c.checked = false;
                    } else {
                        d.ABONO = d.TOT;
                        setCargo(d);
                        sumCargos();
                    }
                } else {
                    delCargo(d);
                }
            }
            setAbono = function(c, idObj) {
                var d = cargos.get(idObj);
                if ((c.value * 1) > (d.TOT * 1)) {
                    Dialogs.close();
                    Dialogs.alert("El abono debe ser igual o menor al cargo");
                    c.className = "errorField";
                    c.focus();
                    return;
                }
                c.className = "";
                d.ABONO = c.value;
                sumCargos();
            }

            setCargo = function(cargo) {
                cargos.set(cargo.ID, cargo);
            }

            delCargo = function(cargo) {
                cargos.unset(cargo.ID);
                sumCargos();
                txt = $("txt_" + cargo.ID)
                txt.className = "";
                txt.value = parseFloat(cargo.TOT);

                if (cargos.values().size() === 0) {
                    moneda = null;
                }
            }

            sumCargos = function() {
                var monto = 0;
                cargos.each(function(i) {
                    if (i.value.checked) {
                        monto += parseFloat(i.value.ABONO);
                    }
                });
                $('MONTO').value = parseFloat(monto);
            }

            datosEstud = function() {
                $('operation').value = 'id';
                $(document.ASIGNACION).request({onComplete: function(r) {
                        res = r.responseJSON;
                        if (res.ERROR) {
                            alert(res.ERROR);
                        } else {
                            $('ACCOUNT').value = res.AC;
                            loadCargos();
                        }
                        closeWait();
                    }});
            }

            loadCargos = function() {
                $('cargos').innerHTML = 'Cargando...';
                var a = new Ajax.Updater('cargos', 'payments.php', {method: 'get',
                    parameters: Form.serialize($(document.ASIGNACION)),
                    onComplete: function(r) {
                        $('cargos').update(r.responseText);
                        $('refresh').disabled = false;
                    }});
            }


            var rsv;
            config = function() {
                rsv = new RSV({
                    formID: $(document.ASIGNACION),
                    errorFieldClass: "errorField",
                    rules: ["digits_only,ID,Carnet es numerico!",
                        "required,NAME,Debe buscar un estudiante!"],
                    displayType: "display-html",
                    errorHTMLItemBullet: "&#8212; ",
                    onCompleteHandler: function() {
                        if (cargos.values().length == 0) {
                            Dialogs.alert('debe seleccionar algun cargo a pagar!');
                        } else {
                            $('operation').value = 's';
                            $('json').value = JSON.stringify(cargos.values());
                            $(document.ASIGNACION).submit();
                        }
                    }
                });
            }

            depurarCargos = function() {
                cargos.each(function(i) {
                    if (i.value.checked) {
                        if (!$("text_" + i.value.ID).className.empty()) {
                            delCargo(i.value);
                        }
                    }
                });
            }

            addOnLoad(config);
            addOnLoad(function() {
                var f = new Hash({tittle: 'Listado de Alumnos',
                    width: 500, height: 500, url: 'data.php?operation=stud', callback: selStudent});
                setList($('buscar'), f.toObject());
            });

            selStudent = function(s) {
                $("ID").value = s.CARNET;
                $("NAME").value = s.NOMBRE_A + ' ' + s.NOMBRE_B + ' ' + s.APELLIDOS;
                $("MONTO").value = 0.00;
                cargos = new Hash();
                datosEstud();
            }
        </script>
    </head>

    <body>
        <div id="rsvErrors"></div>

        <form id="ASIGNACION" name="ASIGNACION" method="GET" action="data.php" >
            <table class=""  width="100%" border="0" cellspacing="0" cellpadding="0">
                <tr>
                    <th width="100%" colspan="2">
                        <ul class="nav nav-tabs">
                            <li class="active">
                                <a>Cobro de cargos</a>
                            </li>
                        </ul>
                    </th>
                </tr>
                <tr>    
                    <th width="50%" valign="middle"></th>
                    <th width="50%">
                        <div class="btn-group">
                            <input type="button" name="cmdOk" id="cmdOk" class="btn btn-info"  value="Realizar Pago" onclick="rsv.validate();" />
                            <input name="cancelar" type="button"  class="btn btn-info" id="cancelar" onclick="this.form.action = 'index.php';
                this.form.submit();" value="Cancelar" />
                        </div>
                    </th>
                </tr>
                <tr>
                    <td colspan="2"><br/><table width="100%" border="0" cellspacing="0" cellpadding="0">
                            <tr>
                                <td width="20%"> NO. CARNET</td>
                                <td width="80%">            <input name="ID" type="text" id="ID" readonly="readonly" />
                                    <input name="buscar" type="button" class="btn btn-small" id="buscar"  value="..." /></td>
                            </tr>              
                            <tr>
                                <td>NOMBRE ESTUDIANTE</td>
                                <td><input name="NAME" type="text" id="NAME" readonly="readonly" size="50" />
                                    <input type="hidden" name="ACCOUNT" id="ACCOUNT" />
                                    <input type="hidden" name="operation" id="operation" />
                                    <input type="hidden" name="json" id="json" /></td>
                            </tr>
                            <tr>
                                <td>FECHA PAGO</td>
                                <td><input name="FECHAPAGO" value="<?php echo date("Y-m-d"); ?>" readonly="readonly" type="text" id="FECHAPAGO" size="30" />
                                    <script type="text/javascript">
            new Control.DatePicker('FECHAPAGO', {icon: '../../js/protocalendar/calendar.png', dateFormat: 'yyyy-MM-dd'});
                                    </script>          </td>
                            </tr>
                            <tr>
                                <td>MONTO</td>
                                <td><input
                                        name="MONTO" type="text" value="" style="text-align:right;" id="MONTO" size="30" maxlength="100" /></td>
                            </tr>
                            <tr>
                                <td>&nbsp;</td>
                                <td ><input disabled="disabled" name="refresh" onclick="loadCargos();" type="button" class="btn btn-info" id="refresh" value="Recalcular" /></td>
                            </tr>



                        </table>

                    </td>
                </tr>

            </table>
            <br/>
            <table  width="100%" border="0" cellspacing="0" cellpadding="0">
                <tr>
                    <th scope="col" style="text-align: left"><legend>Pagos Pendientes del Alumno</legend></th>
                </tr>
                <tr>
                    <td colspan="2" id="cargos">&nbsp;</td>
                </tr>
            </table>
        </form>

    </body>
</html>
